<template>
  <div class="dashboard-editor-container">
    <header>
      <div class="header-container">
        <div class="header-title">
          <h1 class="pulll_left">
            {{ title || "平台" }}
          </h1>
        </div>
      </div>
    </header>
    <main class="grid" style="margin-top: 10px">
      <div class="item item_grid">
        <div class="chartBox chartLeft boxTips">
          <div>
            <h3>{{ devStats.total_count || 0 }}</h3>
            <p>设备总数</p>
          </div>
          <div>
            <h3>{{ devStats.running_count || 0 }}</h3>
            <p>运行设备</p>
          </div>
          <div>
            <h3>{{ devStats.repair_count || 0 }}</h3>
            <p>检修设备</p>
          </div>
          <div>
            <h3>{{ devStats.abnormal_count || 0 }}</h3>
            <p>异常设备</p>
          </div>
        </div>
        <div class="chartBox chartRight" style="margin-top: 10px">
          <h4 style="position: relative">
            设备信息
            <div class="tab-bar">
              <span
                v-for="tab in tabs"
                :key="tab.key"
                :class="{ active: activeTab === tab.key }"
                @click="activeTab = tab.key"
                class="tab-item"
              >
                {{ tab.label }}
              </span>
            </div>
          </h4>
          <DeviceInfo :noticeType="activeTab" />
        </div>
        <div class="chartBox chartLeft" style="margin-top: 10px">
          <h4>并网点分布统计</h4>
          <SimCard />
        </div>
      </div>
      <div class="item map item_grid">
        <MapChart class="map" />

        <div class="chartBox chartLeft" style="margin-top: 10px">
          <h4>变电站分布统计</h4>
          <FactoryStation />
        </div>
      </div>
      <div class="item item_grid">
        <div class="chartBox chartLeft boxTips" style="position: relative;display:block;padding: 18px">
          <div class="quick-select">
            <span
              v-for="t in quickRanges"
              :key="t.value"
              :class="{ active: activeQuick === t.value }"
              @click="activeQuick = t.value"
              class="quick-item"
            >
              {{ t.label }}
            </span>
          </div>
          <div style="display:flex;justify-content:space-between;">
            <div>
              <h3>{{ sumData.deviceCount || 0 }}</h3>
              <p>{{activeQuick}}天新增设备</p>
            </div>
            <div>
              <h3>{{ sumData.substationCount || 0 }}</h3>
              <p>{{activeQuick}}天新增变电站</p>
            </div>
          </div>
          <!-- <div>
            <h3>2190</h3>
            <p>运行设备</p>
          </div>
          <div>
            <h3>108</h3>
            <p>异常设备</p>
          </div> -->
        </div>
        <div
          class="chartBox chartLeft"
          style="
            margin-top: 10px;
            border: 0;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
          "
        >
          <StationStat />
          <!-- <Substation /> -->
        </div>
        <div class="chartBox chartRight" style="margin-top: 10px">
          <h4
            style="padding-top: 6px; padding-bottom: 6px"
            class="period-toggle"
          >
            新增变电站统计
            <span class="toggle-group">
              <button
                v-for="p in periods"
                :key="p.value"
                :class="{ active: activePeriod === p.value }"
                @click="activePeriod = p.value"
                class="toggle-btn"
              >
                {{ p.label }}
              </button>
            </span>
          </h4>
          <Company :period="activePeriod" />
        </div>
      </div>
    </main>
  </div>
</template>

<script>
import MapChart from "@/views/dashboard/MapChart.vue";
import Substation from "@/components/Substation";
import FactoryStation from "@/components/FactoryStation";
import DeviceInfo from "@/components/DeviceInfo"; // Import the DeviceInfo component
import SimCard from "@/components/SimCard"; // Import the DeviceInfo component
import Company from "@/components/Company"; // Import the DeviceInfo component
import request from "@/utils/request";
import StationStat from "@/components/stationStat";

export default {
  name: "Index",
  components: {
    Substation,
    MapChart,
    FactoryStation,
    DeviceInfo,
    SimCard,
    Company,
    StationStat,
  },
  data() {
    return {
      periods: [
        { label: "年", value: "year" },
        { label: "季", value: "quarter" },
        { label: "月", value: "month" },
        { label: "周", value: "week" },
      ],
      tabs: [
        { key: "1", label: "异常设备监控" },
        { key: "2", label: "异常电话卡监控" },
      ],
      activeTab: "1",
      activeQuick: "365",
      quickRanges: [
        { label: "365天", value: "365" },
        { label: "90天", value: "90" },
        { label: "30天", value: "30" },
        { label: "24小时", value: "1" },
      ],
      activePeriod: "month",
      devStats: {},
      sumData: {},
      title: "",
      // 350头部，70提示框
      // 如果设置隐藏top-tags，则还需要减去35
      // lineHeight: document.documentElement.clientHeight - 350 - 70 + "px", //实时屏幕高度

      lineHeight: document.documentElement.clientHeight - 350 + "px", //实时屏幕高度
    };
  },
  watch: {
    activeQuick: {
      handler(newVal, oldVal) {
        this.fetchData();
      },
      immediate: true,
    },
  },
  methods: {
    fetchData() {
      request({
        url: "/zk/substation/monthly-additions",
        method: "get",
        params: {
          timeRange: this.activeQuick,
        },
      }).then((res) => {
        this.sumData = res.data;
      });
    },
  },
  mounted() {
    request({
      url: "/api/cmsApi/getSystemInfo",
      method: "get",
    }).then((res) => {
      console.log("result", res);
      this.title = res.data.sysdiagram;
    });
    request({
      url: "/zk/device/device/stats",
      method: "get",
    }).then((res) => {
      this.devStats = res.data;
    });
  },
};
</script>

<style lang="scss" scoped>
.tab-bar {
  display: flex;
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  gap: 6px;
  margin-left: auto;
}

.tab-item {
  padding: 4px 10px;
  font-size: 12px;
  background: transparent;
  color: #3dcffa;
  cursor: pointer;
  border-radius: 3px;
  transition: all 0.2s ease;

  &:hover {
    background: rgba(7, 118, 181, 0.2);
  }

  &.active {
    background: rgba(22, 214, 255, 0.25);
    color: #fff;
    box-shadow: 0 0 6px rgba(22, 214, 255, 0.6);
  }
}

.quick-select {
  // position: absolute;
  top: 6px;
  left: 20px;
  display: flex;
  gap: 6px;
  z-index: 1;
  margin-bottom: 10px;
}

.quick-item {
  padding: 2px 8px;
  font-size: 12px;
  border: 1px solid rgba(7, 118, 181, 0.8);
  background: rgba(0, 0, 0, 0.25);
  color: #3dcffa;
  cursor: pointer;
  border-radius: 3px;
  transition: all 0.2s ease;

  &:hover {
    background: rgba(7, 118, 181, 0.2);
  }

  &.active {
    background: rgba(22, 214, 255, 0.25);
    color: #fff;
    border-color: #16d6ff;
    box-shadow: 0 0 6px rgba(22, 214, 255, 0.6);
  }
}

.period-toggle {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 10px 10px 25px;
  border-bottom: 1px solid rgba(7, 118, 181, 0.7);
  position: relative;
  color: #3dcffa;

  &::before {
    position: absolute;
    content: "";
    width: 6px;
    height: 6px;
    background: rgba(22, 214, 255, 0.9);
    box-shadow: 0 0 5px rgba(22, 214, 255, 0.9);
    border-radius: 10px;
    left: 10px;
    top: 15px;
  }

  &::after {
    width: 100%;
    height: 1px;
    content: "";
    position: absolute;
    left: 0;
    bottom: -1px;
    background: linear-gradient(to right, #076ead, #4ba6e0, #076ead);
    box-shadow: 0 0 5px rgba(131, 189, 227, 1);
    opacity: 0.6;
  }
}

.toggle-group {
  display: flex;
  gap: 6px;
}

.toggle-btn {
  padding: 4px 10px;
  font-size: 12px;
  border: 1px solid rgba(7, 118, 181, 0.8);
  background: transparent;
  color: #3dcffa;
  cursor: pointer;
  border-radius: 3px;
  transition: all 0.2s ease;

  &:hover {
    background: rgba(7, 118, 181, 0.2);
  }

  &.active {
    background: rgba(22, 214, 255, 0.25);
    color: #fff;
    border-color: #16d6ff;
    box-shadow: 0 0 6px rgba(22, 214, 255, 0.6);
  }
}

.boxTips {
  display: flex;
  justify-content: space-between;
  padding: 20px;
  h3 {
    margin: 0;
    color: #fff;
    font-size: 20px;
  }
  p {
    margin: 0;
    margin-top: 10px;
    font-size: 13px;
    color: #33a9d1;
  }
}
.pulll_left {
  font-size: 1.1vw;
  letter-spacing: 1px;
  color: #3dcffa;
  transform: translateY(10px);
  margin: 0;
}

.header-title {
  background-image: url("~@/assets/images/title.png");
  height: 50px;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  text-align: center;
  font-size: 18px;
}

.map {
  border: 1px solid rgba(7, 118, 181, 0.5);
  box-shadow: inset 0 0 10px rgba(7, 118, 181, 0.4);
  height: 577px;
  background-image: url("~@/assets/images/back.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
}

h4 {
  margin: 0;
  padding: 10px 0;
  border-bottom: 2px solid #ccc;
}

.dashboard-editor-container {
  // background: linear-gradient(135deg, #72EDF2 10%, #5151E5 100%);
  background: -webkit-radial-gradient(
    50% 35%,
    farthest-corner,
    #101229,
    #101229,
    #101229,
    #002353
  );
  padding: 20px 32px;
  width: 100%;
}

.grid {
  display: grid;
  grid-template-columns: 1fr 40% 1fr;
  // grid-template-rows: calc(100vh - 170px);
  grid-row-gap: 10px;
  grid-column-gap: 10px;
  // grid-template-areas: "header header header"
  //   "main main sidebar"
  //   "footer footer footer";

  .item {
    width: 100%;
  }

  .item_grid {
    .chartBox {
      width: 100%;
      border: 1px solid rgba(7, 118, 181, 0.5);
      box-shadow: inset 0 0 10px rgba(7, 118, 181, 0.4);

      h4 {
        padding: 10px 10px 10px 25px;
        border-bottom: 1px solid rgba(7, 118, 181, 0.7);
        position: relative;
        color: #3dcffa;

        &::before {
          position: absolute;
          content: "";
          width: 6px;
          height: 6px;
          background: rgba(22, 214, 255, 0.9);
          box-shadow: 0 0 5px rgba(22, 214, 255, 0.9);
          border-radius: 10px;
          left: 10px;
          top: 15px;
        }

        &::after {
          width: 100%;
          height: 1px;
          content: "";
          position: absolute;
          left: 0;
          bottom: -1px;
          background: linear-gradient(to right, #076ead, #4ba6e0, #076ead);
          box-shadow: 0 0 5px rgba(131, 189, 227, 1);
          opacity: 0.6;
        }
      }
    }
  }
}
</style>
